package launcher;

import java.util.ArrayList;
import java.util.List;

public class OptionManager {

	public String [] args = null;
	
	private int mode;	
	private String rcDir;
	private String environment;
	private String packageDir;
	private List<String> remoteLauncherCommands;
	
	public static final int OK = 0;
	public static final int NO_ARGS = 1;
	public static final int NO_MODE = 2;
	public static final int RCMODE_NOARGS = 3;
	public static final int RCMODE_NORCDIR = 4;
	public static final int NORCMODE_NOARGS = 5;
	public static final int NORCMODE_NOCOMMANDS = 6;
	
	public static final int OP_CREATE = 1;
	public static final int OP_SINC = 2;
	public static final int OP_COPY = 3;
	public static final int OP_LIST = 4;
	public static final int OP_RL = 5;
	
	public OptionManager (String [] args){
		this.args = args;
	}
	
	public int checkArgs(){
		if (args.length == 0) {
			return NO_ARGS;
		}
		
		// First arg always must be the run mode
		if (args[0].equals("-create")) {
			mode = OP_CREATE;
			return OK;
		} else if (args[0].equals("-sync")) {
			mode = OP_SINC;
			return checkRCArgs();
		} else if (args[0].equals("-copy")) {
			mode = OP_COPY;
			return checkRCArgs();
		} else if (args[0].equals("-list")) {
			mode = OP_LIST;
			return checkRCArgs();
		} else if (args[0].equals("-rl")) {
			mode = OP_RL;
			return checkNoRCArgs();
		} else {
			return NO_MODE;
		}
	}
	
	private int checkRCArgs(){
		if (args.length <= 1) {
			return RCMODE_NOARGS;
		}

		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-d") && (i + 1 < args.length))
				rcDir = args[i + 1];
			if (args[i].equals("-e") && (i + 1 < args.length))
				environment = args[i + 1];
		}
		
		if(rcDir == null)
			return RCMODE_NORCDIR;
		
		return OK;
	}
	
	private int checkNoRCArgs(){
		if (args.length <= 1) {
			return NORCMODE_NOARGS;
		}
		
		remoteLauncherCommands = new ArrayList<String>();
		
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-chu") || args[i].equals("-chb") || args[i].equals("-iu") || args[i].equals("-ib") || args[i].equals("-cucust") || args[i].equals("-cucore"))
				remoteLauncherCommands.add(args[i].substring(1));
			if (args[i].equals("-e") && (i + 1 < args.length))
				environment = args[i+1];
		}
		
		if(remoteLauncherCommands.isEmpty())
			return NORCMODE_NOCOMMANDS;
		
		return OK;
	}

	public int getMode(){
		return mode;
	}
	
	public String getRcDir() {
		return rcDir;
	}

	public String getEnvironment() {
		if(environment == null)
			environment = "Mant";
		return environment;
	}

	public String getPackageDir() {
		return packageDir;
	}

	public List<String> getRemoteLauncherCommand() {
		return remoteLauncherCommands;
	}
}
